package algorithm.t202111;

import java.util.Arrays;

/**
 * @author : 李红磊
 * @version :1.0
 * @date : 2021/11/23 8:48
 * @description :1道
 * 忙于开题报告的一天。（忘记上传
 * persevere to last
 */
public class t20211123 {

    //859.亲密字符串
    public boolean buddyStrings(String s, String goal) {
        int n = s.length(), m = goal.length();
        if (n != m) return false;
        int[] cnt1 = new int[26], cnt2 = new int[26];
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int a = s.charAt(i) - 'a', b = goal.charAt(i) - 'a';
            cnt1[a]++; cnt2[b]++;
            if (a != b) sum++;
        }
        boolean ok = false;
        for (int i = 0; i < 26; i++) {
            if (cnt1[i] != cnt2[i]) return false;
            if (cnt1[i] > 1) ok = true;
        }
        return sum == 2 || (sum == 0 && ok);
    }

    public static void main(String[] args) {
        t20211123 t20211123 = new t20211123();
        System.out.println(t20211123.buddyStrings("ab", "cd"));


    }



}
